Voice echo in UDP based voice transmission [closed]

Posted by Meherzad on Programmers See other posts from Programmers or by Meherzad
Published on 2012-04-06T13:54:29Z Indexed on 2012/04/06 17:42 UTC
Read the original article Hit count: 463

Filed under:

I have coded a java application for voice transmission between to ip in LAN.

Here the code.

     public static Boolean flag= true;
    public static Boolean recFlag=true;
    DatagramSocket UDPSocket=null;
    AudioFormat format = null;
 TargetDataLine microphone=null;
 byte[] buffer=null;
 DatagramPacket UDPPacket=null;



 public void startChat(String ipAddress){
    try{
        buffer = new byte[1000];
        UDPSocket=new DatagramSocket(1987);

        Thread th=new Thread(new Listener());
        th.start();
        microphone = AudioSystem.getTargetDataLine(format);
        format=  new AudioFormat(8000.0f, 16, 1, true, true);
        UDPPacket = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(ipAddress), 1988);
        microphone.open(format);
        microphone.start();

        while (flag) {                
            microphone.read(buffer, 0, buffer.length);
            UDPSocket.send(UDPPacket);

        }

    }
    catch(Exception e){
        System.out.println(" ssss "+e.getMessage());

    }
 }



   public class Listener extends Thread{
   byte[] buff=new byte[1000];
   DatagramSocket UDPSocket1=null;
   DatagramPacket recPacket=null;
  DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
   SourceDataLine line=null;


     @Override
     public void run(){

         try{
             UDPSocket1=new DatagramSocket(1988);
            format=  new AudioFormat(8000.0f, 16, 1, true, true);

            line = (SourceDataLine)   AudioSystem.getLine(info);
            line.open(format);
            line.start();

        }
        catch(Exception e){
            System.out.println("list "+ e.getMessage());
        }

         recPacket=new DatagramPacket(buff, buff.length);
         while(recFlag){
             try{
                UDPSocket1.receive(recPacket);
                buff = (byte[])recPacket.getData();
                line.write(buff, 0, buff.length);

             }
             catch(Exception e){
                 System.out.println("errr "+e.getMessage());

             }

          }
            line.drain();
             line.close();

     }
 }

Main problem which I am facing that I am getting only echo of my own voice. I am unable to hear voice from the other end only I am hearing is my own voice. Please suggest any solution.

© Programmers or respective owner

Related posts about java